package com.loop.unclelend.ui.presenter

import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.ApiService.addCommonParams
import com.loop.unclelend.network.ApiService.addRandomParams
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Home
import com.loop.unclelend.utils.event.UncleEventData
import com.loop.unclelend.utils.helper.SharedHelper
import com.loop.unclelend.utils.helper.UncleStatusHelper
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/10 15:15
 * @Annotation：首页
 */
class Pre_Home(private val iView: IView_Home): ProPresenter(iView) {


    /**
     * 获取总流程
     */
    fun requestProcessAction() {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path", ApiService.API_PATH.UNCLE_API_PROCESS_PATH)
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                //保存用户流程状态
                if (obj != null) {
                    // TODO: 测试添加 Step 类型
//                    val allStepTitles = obj.optJSONArray("allStepTitles")
//                    allStepTitles.put("Ocr Info")
//                    allStepTitles.put("Face recognition")
//                    allStepTitles.put("Account Info")
//                    val allSteps = obj.optJSONArray("allSteps")
//                    allSteps.put("ocr_info")
//                    allSteps.put("sefie_info")
//                    allSteps.put("account_info")
//                    val steps = obj.optJSONArray("steps")
//                    steps.put("account_info")
//                    val index = Random.nextInt(3)
//                    when(index){
//                        0 -> {
//                            steps.put("ocr_info")
//                        }
//                        1 -> {
//                            steps.put("sefie_info")
//                        }
//                        else -> {
//                            steps.put("account_info")
//                        }
//                    }

                    SharedHelper.saveUncleSPData(SharedHelper.UNCLE_SPKEY.UNCLE_PROCESS, obj.toString())
                    //通知全局流程刷新完成
                    iView.getAct().publishEventData(UncleEventData(UncleEventData.TYPE.UNCLE_PROCESS_REFRESHED))

                    //申请状态
                    val applyStatus = obj.optString("applyStatus","")
                    //订单状态
                    val orderStatus = obj.optString("orderStatus","")

                    //是否在审核中 || 放款中 || 已拒绝
                    val isReviewing = UncleStatusHelper.StatusRepository(applyStatus,orderStatus).isReviewing()
                    val isDisbursing = UncleStatusHelper.StatusRepository(applyStatus,orderStatus).isDisbursing()
                    val isRejected = UncleStatusHelper.StatusRepository(applyStatus,orderStatus).isRejected()
                    iView.hideUncleAllPages()
                    if (isReviewing || isDisbursing || isRejected){
                        iView.showUncleOrderStatusView()
                        return
                    }
                    iView.showUncleLoanLimitView()
                }
            }
            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }

}